package com.maycur.ctrip.mybatis.typehandler;

import com.maycur.ctrip.enumeration.PayType;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by zhengpeiyu on 6/7/16.
 */
public class PayTypeHandler extends BaseTypeHandler<PayType> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,  PayType parameter, JdbcType jdbcType)
        throws SQLException {
        ps.setString(i, parameter.name());
    }

    @Override
    public PayType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return PayType.fromName(rs.getString(columnName));
    }

    @Override
    public PayType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String formSource = rs.getString(columnIndex);
        return formSource == null ? null : PayType.fromName(formSource);
    }

    @Override
    public PayType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
